package com.joker.controller;

import com.joker.common.GlobalInteger;
import com.joker.common.GlobalMap;
import com.joker.discovery.InformationDiscovery;
import com.joker.entity.User;
import com.joker.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 控制用户上线，下线 前端控制器
 * </p>
 *
 * @author joker
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/user")
@Api(value="UserController",tags={"用户信息操作接口"})
public class UserHandler {
    @Autowired
    private InformationDiscovery informationDiscovery;
    /**
     * 用户上线
     */
    @ApiOperation("用户登录系统接口")
    @GetMapping("/online/{userId}")
    public ResultUtil online(@PathVariable("userId")String userId){
        if(GlobalMap.sessionPool.get(userId) != null){
            return ResultUtil.error("用户在其他客户端登录");
        }else {
            GlobalInteger.onlineNumber++;
            System.out.println("目前在线人数："+GlobalInteger.onlineNumber);
            return ResultUtil.success("登录成功！");
        }
    }
    /**
     * 获取用户信息
     */
    @ApiOperation("用户登录系统后获取用户信息")
    @GetMapping("/findByUserId/{userId}")
    public ResultUtil findByUserId(@PathVariable("userId")String userId){
        User user = informationDiscovery.instances(userId);
        if(user != null){
            return ResultUtil.success(user);
        }
        return ResultUtil.error("找不到该用户");
    }
}
